package com.dmp.onlinestore.apps;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class TestMySqlApp {

	public static void main(String[] args) {
		System.out.println("Hello World.");
        Connection conn = null;
    	Statement stmt  = null;
    	ResultSet rs    = null;
    	try {
			Class.forName("com.mysql.jdbc.Driver");
        	conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlinestore?" +
                                      "user=root&password=password");
        	stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * from PRODUCT");
            while (rs.next()) {
            	String name = rs.getString("name");
            	System.out.println("Name is \"" + name + "\"");
            }

//        	conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mylibrary?" +
//            "user=root&password=password");
//        	stmt = conn.createStatement();
//        	rs = stmt.executeQuery("SELECT author_name from my_authors");
//        	while (rs.next()) {
//        		String name = rs.getString("author_name");
//        		System.out.println("Name is " + name);
//        	}
    	
    	} 
        catch (SQLException ex) {
	        // handle any errors
	        System.out.println("SQLException: " + ex.getMessage());
	        System.out.println("SQLState: " + ex.getSQLState());
	        System.out.println("VendorError: " + ex.getErrorCode());
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        finally {
            // it is a good idea to release resources in a finally{} block
            // in reverse-order of their creation if they are no-longer needed
            if (rs != null) {
                try {
                    rs.close();
                } 
                catch (SQLException sqlEx) { } // ignore
                rs = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } 
                catch (SQLException sqlEx) { } // ignore
                stmt = null;
            }
        } // finally
	}
}
